<?php

class yourcart extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('msanpham');
        $this->load->library("loadpage");
    }

    public function index() {
        $lp = new loadpage();
        $lp->load(array('v_yourcart'), null);
    }

    public function add() {

        $sp = new MSanpham();
        $id = trim($_GET['id']);
        $sp = $sp->getByID($id);
        $qty = trim($_GET['qty']);
        $price = trim($sp['giaban']);
        $name = trim($sp['tensp']);
        if ($this->check($id, $qty)) {
            echo 'trungf';
            $data2 = array(
                'id' => $id,
                'qty' => $qty,
                'price' => $price,
                'name' => $name
            );

            $this->cart->insert($data2);
            echo 'succes';
        }
        // else
    }

    function check($id, $qty) {
        $data = $this->cart->contents();
        foreach ($this->cart->contents() as $item) {
            if ($item['id'] == $id) {
                $num = intval($item['qty']) + intval($qty);
                echo $num;
                $cartitem = array('rowid' => $item['rowid'], 'qty' => $num);
                $this->cart->update($cartitem);
                return false;
            }
        }
        return true;
    }

    public function update() {
        $qty = trim($_GET['qty']);
        $id = trim($_GET['id']);
        $data = $this->cart->contents();
        foreach ($this->cart->contents() as $item) {
            if ($item['id'] == $id) {
                $num = intval($qty);
                $cartitem = array('rowid' => $item['rowid'], 'qty' => $num);
                $this->cart->update($cartitem);
                return true;
            }
        }
        return FALSE;
    }

    public function update1() {
        $rowid = trim($_GET['rowid']);
        $qty = trim($_GET['qty']);
        $dt = array('rowid' => $rowid, 'qty' => $qty);
        $this->cart->update($dt);
        // $this->load->view('listsanpham_view');
    }

    public function delete() {
        $id = trim($_GET['id']);
        $qty = 0;
        $data = $this->cart->contents();
        foreach ($this->cart->contents() as $item) {
            if ($item['id'] == $id) {
                $num = 0;
                $cartitem = array('rowid' => $item['rowid'], 'qty' => $num);
                $this->cart->update($cartitem);
                $result = array('delete1' => TRUE);
                 echo json_encode($result);return;
            }
        }
        $result = array('delete1' => FALSE);
        echo json_encode($result); return;
    }

    public function delete1() {
        $rowid = trim($_GET['rowid']);
        $qty = 0;
        $dt = array('rowid' => $rowid, 'qty' => $qty);
        $this->cart->update($dt);
        $this->load->view('listsanpham_view');
    }

    public function removeall() {
        // var_dump($this->cart->destroy());
        $this->session->sess_destroy();
    }

}

?>
